package patrol.mobile.com.mobilepatrol.activity.security.emergency;


import android.Manifest;
import android.app.AlertDialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyProtectListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.ProtectListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;

/**
 * 业务办理模块
 */
public class EmergencyProtectListActivity extends CommonListActivity implements CallBack<DateBean> {
    private List<String> orgNameList = new ArrayList<>();
    private List<String> orgIdList = new ArrayList<>();
//    private View itemTopView;
//    private MySpinner spOrgId;
//    private MySpinner spStartTime;
//    private MySpinner spEndTime;

    private int clickFlag;//点击选择时间的标志 1是开始时间 2是结束时间
    private String currentOrgId = "";
    private String currentStartTime = "";
    private String currentEndTime = "";
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private int page = 1;
    private List<ProtectListBean> protectListBeanList = new ArrayList<>();

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencyProtectListAdapter(this);
    }

    @Override
    public void initView() {
        super.initView();
//        itemTopView = LayoutInflater.from(this).inflate(R.layout.activity_emergency_protect_list_top, null);
//        spOrgId = itemTopView.findViewById(R.id.spOrgId);
//        spStartTime = itemTopView.findViewById(R.id.spStartTime);
//        spEndTime = itemTopView.findViewById(R.id.spEndTime);
//        //flContent.addView(itemTopView);//业务模块去掉查询
//        spOrgId.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
//        spStartTime.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
//        spEndTime.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_PROTECT_LIST;
    }

    @Override
    public void initTitle() {
        super.initTitle();
        iv_right_title.setVisibility(View.GONE);
        iv_right_title.setVisibility(View.VISIBLE);
        iv_right_title.setImageDrawable(getResources().getDrawable(R.mipmap.sys));
    }

    @Override
    protected void onResume() {
        super.onResume();
        getOrg();
    }

    @Override
    public void setListener() {
        super.setListener();
        iv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (AndPermission.hasPermission(EmergencyProtectListActivity.this, Manifest.permission.CAMERA)) {
                    Intent intent = new Intent(EmergencyProtectListActivity.this, EmergencySignScanActivity.class);
                    intent.putExtra("sigin", (ArrayList) protectListBeanList);
                    startActivity(intent);
                } else {
                    AndPermission.with(EmergencyProtectListActivity.this)
                            .requestCode(100)
                            .permission(Manifest.permission.CAMERA)
                            .rationale(new RationaleListener() {
                                @Override
                                public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                                    // Storage permission are allowed.
                                    new AlertDialog.Builder(EmergencyProtectListActivity.this)
                                            .setTitle("友好提醒")
                                            .setMessage("你是否要继续打开相机权限！")
                                            .setPositiveButton("好，给你", (dialog, which) -> {
                                                rationale.resume();
                                            })
                                            .setNegativeButton("我拒绝", (dialog, which) -> {
                                                rationale.cancel();
                                            }).show();
                                }
                            })
                            .send();
                }


            }
        });

        currentOrgId = TextUtils.isEmpty(UserManager.getOrgNo()) ? "" : UserManager.getOrgNo();

//        spStartTime.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                clickFlag = 1;
//                DatePickerUtil.getInstance(EmergencyProtectListActivity.this).setCallBack(EmergencyProtectListActivity.this);
//            }
//        });
//
//        spEndTime.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                clickFlag = 2;
//                DatePickerUtil.getInstance(EmergencyProtectListActivity.this).setCallBack(EmergencyProtectListActivity.this);
//            }
//        });
//
//        spStartTime.getmText().setText(sdf.format(new Date()));
//        spEndTime.getmText().setText(sdf.format(new Date()));
//
//        currentStartTime = spStartTime.getmText().getText().toString();
//        currentEndTime = spEndTime.getmText().getText().toString();

        requestData(currentOrgId, "", "");
    }

    @Override
    public void configAdapter() {
        super.configAdapter();


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 100: { // 这个400就是你上面传入的数字。
                // 你可以在这里检查你需要的权限是否被允许，并做相应的操作。
                Intent intent = new Intent(EmergencyProtectListActivity.this, EmergencySignScanActivity.class);
                intent.putExtra("sigin", (ArrayList) protectListBeanList);
                startActivity(intent);
                break;
            }
        }
    }


    private void getOrg() {
//        HashMap<String, String> hashMap = new HashMap();
//        hashMap.put("objType", "分局,总局");
//        hashMap.put("orgId", UserManager.getOrgNo());
//        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
//        sender.requestPost(hashMap, new HttpCallBack(sender) {
//            @Override
//            public void onError(String code, Call call, Exception e) {
//
//            }
//
//            @Override
//            public void onResponse(final String code, String response) {
//                final OrgListBean orgListBean = new Gson().fromJson(response, OrgListBean.class);
//                orgNameList = new ArrayList<>();
//                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
//                    orgNameList.add(listBean.getOrgName());
//                    orgIdList.add(listBean.getId());
//                }
//               // spOrgId.getmText().setText(orgListBean.getList().get(0).getOrgName());
//
//                spOrgId.setmDatas(orgNameList, new MySpinner.CallBack() {
//                    @Override
//                    public void onSelect(int postion) {
//
//                        String orgNme = spOrgId.getmText().getText().toString();
//                        for (int i = 0; i < orgNameList.size(); i++) {
//                            if (orgNme.equals(orgNameList.get(i))) {
//                                currentOrgId = orgIdList.get(i);
//                                requestData(currentOrgId, currentStartTime, currentEndTime);
//                                return;
//                            }
//                        }
//
//
//                    }
//                });
//
//                String orgNme = spOrgId.getmText().getText().toString();
//                for (int i = 0; i < orgNameList.size(); i++) {!
//                    if (orgNme.equals(orgNameList.get(i))) {
//                        currentOrgId = orgIdList.get(i);
//                        requestData(currentOrgId, currentStartTime, currentEndTime);
//                        return;
//                    }
//                }
//
//
//            }
//        });
    }


    private void requestData(String orgId, String startTime, String endTime) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.IEMERGENCY_PROTECT_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("startDate", startTime);
        map.put("endDate", endTime);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                protectListBeanList = GsonUtil.jsonToList(listArray.toString(), ProtectListBean.class);
                mAdapter.updateItems(protectListBeanList);
            }
        });
    }


    @Override
    public void onBack(DateBean dateBean) {
//        if (clickFlag == 1) {
//            currentStartTime = dateBean.getmDate();
//            spStartTime.getmText().setText(dateBean.getmDate());
//        } else {
//            currentEndTime = dateBean.getmDate();
//            spEndTime.getmText().setText(dateBean.getmDate());
//        }
//        requestData(currentOrgId, currentStartTime, currentEndTime);
    }
}
